<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增任务调度')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: ztree-css" />
    <th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-dispatcher-add">
        <div class="form-group is-required">
            <label class="col-sm-3 control-label is-required">资源库类型：</label>
            <div class="col-sm-8">
                <select id="dpRepoType" name="dpRepoType" th:with="type=${@dict.getType('repo_type')}" class="form-control" required>
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:selected="${dpRepoType == dict.dictValue}"></option>
                </select>
            </div>
        </div>
        <div class="form-group is-required" th:style="'display: '+${dpRepoType == '0' ? 'block' : 'none'}" id="gitRepo">
            <label class="col-sm-3 control-label is-required">git仓库名称：</label>
            <div class="col-sm-8">
                <select id="repositoryIdG" name="repositoryIdG"  class="form-control" required>
<!--                    <option></option>-->
                    <option th:each="repo:${repoListG}" th:value="${repo.repositoryId}" th:text="${repo.repositoryName}"></option>
                </select>
            </div>
        </div>
        <div class="form-group is-required" th:style="'display: '+${dpRepoType == '1' ? 'block' : 'none'}" id="fileRepo">
            <label class="col-sm-3 control-label is-required">文件仓库名称：</label>
            <div class="col-sm-8">
                <select id="repositoryIdF" name="repositoryIdF"  class="form-control" required>
<!--                    <option></option>-->
                    <option th:each="repo:${repoListF}" th:value="${repo.id}" th:text="${repo.name}"></option>
                </select>
            </div>
        </div>
        <input type="hidden" name="repositoryId" id="repositoryId" >
        <div class="form-group is-required" th:style="'display: '+${dpRepoType == '2' ? 'block' : 'none'}" id="dbRepo">
            <label class="col-sm-3 control-label is-required">任务类型：</label>
            <div class="col-sm-8">
                <select id="dpType" name="dpType"  class="form-control" required>
                    <option></option>
                    <option th:each="t:${taskType}" th:value="${t.dictValue}" th:text="${t.dictLabel}"></option>
                </select>
            </div>
        </div>
        <div class="form-group" id="selectDpName">
            <label class="col-sm-3 control-label is-required">选择文件：</label>
            <div class="col-sm-8">
                <input id="dpIds" name="dpIds" class="form-control" type="text" readonly required>
                <button type="button" onclick="selectTranOrJob()">选择文件</button>
            </div>
        </div>
<!--        <div class="form-group is-required">-->
<!--            <label class="col-sm-3 control-label is-required">调度名称：</label>-->
<!--            <div class="col-sm-8">-->
<!--                <input id="dpName" name="dpName" class="form-control" type="text" required>-->
<!--            </div>-->
<!--        </div>-->
        <div class="form-group is-required">
            <label class="col-sm-3 control-label is-required">服务节点：</label>
            <div class="col-sm-8">
                <select id="carteId" name="carteId"  class="form-control" required>
                    <option></option>
                    <option th:each="node:${nodeList}" th:value="${node.nodeId}" th:text="${node.name}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">服务地址：</label>
            <div class="col-sm-8">
                <input name="carteUri" class="form-control" type="text" id="carteUri" required readonly>
            </div>
        </div>
        <div class="form-group is-required">
            <label class="col-sm-3 control-label">参数组：</label>
            <div class="col-sm-8">
                <select id="paramsGroupId" name="paramsGroupId"  class="form-control">
                    <option></option>
                    <option th:each="pg:${pgList}" th:value="${pg.paramsGroupId}" th:text="${pg.paramsGroupName}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">预警等级：</label>
            <div class="col-sm-8">
                <select id="warnLevel" name="warnLevel" th:with="type=${@dict.getType('warn_level')}" class="form-control" required>
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">预警人员：</label>
            <div class="col-sm-8">
                <input id="warnMsgIds" name="warnMsgIds" class="form-control" type="text" readonly>
                <button type="button" onclick="selectWarn()">选择预警人员</button>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<th:block th:include="include :: ztree-js" />
<th:block th:include="include :: bootstrap-select-js" />
<script th:inline="javascript">
    var nodeList = [[${nodeList}]];
    var repoList = [[${repoList}]];
    var repoListF = [[${repoListF}]];
    var repoListG = [[${repoListG}]];
    var dpRepoType = [[${dpRepoType}]];
    var prefix = ctx + "busi/dispatcher";
    $("#form-dispatcher-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            let serialize = $('#form-dispatcher-add').serialize();
            // let warnMsgIds = $('#warnMsgIds').val();
            // if (warnMsgIds !== "") {
            //     serialize += "&warnMsgIds=" + warnMsgIds;
            // }
            $.operate.save(prefix + "/add", serialize);
        }
    }

    $("input[name='createdTime']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    $("input[name='updatedTime']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    $(function (){
        $('.selectpicker').selectpicker();

        if (dpRepoType === 0 || dpRepoType === '0') {
            if (repoListG.length > 0) {
                $("#repositoryId").val(repoListG[0].repositoryId);
            }
        } else if (dpRepoType === 1 || dpRepoType === '1') {
            if (repoListF.length > 0) {
                $("#repositoryId").val(repoListF[0].id);
            }
        }

        $("#dpRepoType").bind("change", function () {
            var dpRepoType = $("#dpRepoType option:selected").val();
            console.log("git资源库弄不好dpRepoType="+dpRepoType);
            if (dpRepoType == '0') {
                $("#gitRepo").show();
                $("#fileRepo").hide();
                $("#dbRepo").hide();
                $("#repositoryId").val($("#repositoryIdG").val());
            } else if (dpRepoType == '1') {
                $("#gitRepo").hide();
                $("#fileRepo").show();
                $("#dbRepo").hide();
                $("#repositoryId").val($("#repositoryIdF").val());
            } else {
                $("#gitRepo").hide();
                $("#fileRepo").hide();
                $("#dbRepo").show();
            }
            $("#dpIds").val(null);
        })

        $("#carteId").bind("change", function () {
            var carteId = $("#carteId option:selected").val();
            if (carteId === undefined || carteId === null || carteId === '') {
                $("#carteUri").val(null);
                return;
            } else {
                $("#carteUri").val();
            }

            for (var i = 0; i < nodeList.length; i++) {
                if (nodeList[i]["nodeId"] === carteId*1) {
                    $("#carteUri").val(nodeList[i]["nodeHost"]);
                }
            }

        });

        $("#repositoryIdF").bind("change", function () {
            $("repositoryId").val($("#repositoryIdF").val());
        })

        $("#repositoryIdG").bind("change", function () {
            $("repositoryId").val($("#repositoryIdG").val());
        })

    });

    function selectWarn() {
        var title = "选择预警人员";
        var url = "/busi/monitor/user";

        selectFiles(title, url, "selectWarn");
    }

    function selectTranOrJob() {
        // 如果是移动端，就使用自适应大小弹窗
        var dpRepoType = $("#dpRepoType").val();
        var dpType = $("#dpType").val();
        if (dpRepoType == '0') {
            let repositoryIdG = $("#repositoryIdG").val();
            if (repositoryIdG === undefined || repositoryIdG===null || repositoryIdG==='') {
                return;
            }
            var title = "GIT资源库_"+$("#repositoryIdG").find("option:selected").text();
            var url ="/busi/repository_file/"+$("#repositoryIdG").val() + "?selectedIds="+$("#dpIds").val();
            selectFiles(title, url, "selectTranOrJob");
        } else if (dpRepoType == '1') {
            let repositoryIdF = $("#repositoryIdF").val();
            if (repositoryIdF === undefined || repositoryIdF===null || repositoryIdF==='') {
                return;
            }
            var title = "文件资源库_"+$("#repositoryIdF").find("option:selected").text();
            var url ="/busi/file/msg/"+$("#repositoryIdF").val() + "?selectedIds="+$("#dpIds").val();
            selectFiles(title, url, "selectTranOrJob");
        } else if (dpRepoType == '2' && dpType == '2') {
            let dpType = $("#dpType").val();
            if (dpType === undefined || dpType===null || dpType==='') {
                return;
            }
            var title = "数据库资源库";
            var url ="/busi/JOB/select" + "?selectedIds="+$("#dpIds").val();
            selectFiles(title, url, "selectTranOrJob");
        }  else if (dpRepoType == '2' && dpType == '1') {
            let dpType = $("#dpType").val();
            if (dpType === undefined || dpType===null || dpType==='') {
                return;
            }
            var title = "数据库资源库";
            var url ="/busi/TRANSFORMATION/select" + "?selectedIds="+$("#dpIds").val();
            selectFiles(title, url, "selectTranOrJob");
        } else {
            // $.operate.open("#dpRepoType").val();
        }
    }

    var selectFiles = function (title, url, type) {
        var width = "", height = "";
        // 如果是移动端，就使用自适应大小弹窗
        if ($.common.isMobile()) {
            width = 'auto';
            height = 'auto';
        } else {
            width = "800px";
            height = ($(window).height() - 50) + "px";
        }

        var index = top.layer.open({
            type: 2,
            area: [width, height],
            fix: false,
            //不固定
            maxmin: true,
            shade: 0.3,
            title: title,
            content: url,
            btn: ['确定', '关闭'],
            // 弹层外区域关闭
            shadeClose: true,
            yes: function(index, layero) {
                var iframeWin = layero.find('iframe')[0];
                var selectIds = iframeWin.contentWindow.submitHandler(index, layero);
                if (type === "selectTranOrJob") {
                    $("#dpIds").val(selectIds);
                } else if (type === "selectWarn") {
                    $("#warnMsgIds").val(selectIds);
                }

                $("#form-dispatcher-add").valid();
            },
            cancel: function(index) {
                return true;
            }
        });
        top.layer.full(index);
    }

</script>
</body>
</html>